package com.skycity.common.log.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.skycity.framework.core.utils.ServletUtils;
import com.skycity.common.log.event.LoginInfoEvent;
import com.skycity.common.log.event.OperLogEvent;
import com.skycity.framework.uaa.api.LogFeign;
import com.skycity.framework.uaa.pojo.entity.SysOperLog;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @Description 监听操作日志
 * @Author YingBo.Dai
 * @Date 2024/12/5
 * @Version 1.0
 */
@Component
@Slf4j
public class OperationLogListener {

    @EventListener
    public void processOperLog(OperLogEvent event) {
        log.info("OperLogEvent监听到事件：{}", event);
        SysOperLog operLog = BeanUtil.toBean(event, SysOperLog.class);
        SpringUtil.getBean(LogFeign.class).add(operLog);
    }

    /**
     * 监听登录日志事件
     * 保存登录日志
     * @param loginInfoEvent
     */
    @EventListener
    public void saveLoginLog(LoginInfoEvent loginInfoEvent) {
        log.info("LoginInfoEvent监听到事件：{}", loginInfoEvent);
        HttpServletRequest request = ServletUtils.getRequest();
        final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
        final String ip = ServletUtils.getClientIP(request);
        // 客户端信息
//        String clientId = request.getHeader(LoginHelper.CLIENT_KEY);
//        RemoteClientVo clientVo = null;
//        if (StringUtils.isNotBlank(clientId)) {
//            clientVo = remoteClientService.queryByClientId(clientId);
//        }

//        String address = IPV4Util.getRemoteAddr().getRealAddressByIP(ip);
        StringBuilder s = new StringBuilder();
//        s.append(getBlock(ip));
//        s.append(address);
//        s.append(getBlock(logininforEvent.getUsername()));
//        s.append(getBlock(logininforEvent.getStatus()));
//        s.append(getBlock(logininforEvent.getMessage()));

        // 打印信息到日志
//        log.info(s.toString(), loginInfoEvent.getArgs());
        // 获取客户端操作系统
        String os = userAgent.getOs().getName();
        // 获取客户端浏览器
        String browser = userAgent.getBrowser().getName();
        // 封装对象
//        RemoteLogininforBo logininfor = new RemoteLogininforBo();
//        logininfor.setTenantId(logininforEvent.getTenantId());
//        logininfor.setUserName(logininforEvent.getUsername());
//        if (ObjectUtil.isNotNull(clientVo)) {
//            logininfor.setClientKey(clientVo.getClientKey());
//            logininfor.setDeviceType(clientVo.getDeviceType());
//        }
//        logininfor.setIpaddr(ip);
//        logininfor.setLoginLocation(address);
//        logininfor.setBrowser(browser);
//        logininfor.setOs(os);
//        logininfor.setMsg(logininforEvent.getMessage());
        // 日志状态
//        if (StringUtils.equalsAny(logininforEvent.getStatus(), Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
//            logininfor.setStatus(Constants.SUCCESS);
//        } else if (Constants.LOGIN_FAIL.equals(logininforEvent.getStatus())) {
//            logininfor.setStatus(Constants.FAIL);
//        }
//        remoteLogService.saveLogininfor(logininfor);


    }
}
